/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sabatec.service;

import br.com.sabatec.entities.RequestServiceSabatec;
import br.com.sabatec.persistence.JpaUtil;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.primefaces.context.RequestContext;

/**
 * 
 * @author Daniel.Morais
 */
public class RequestSabatecService {

	public static void salva(RequestServiceSabatec request) throws Exception {

		EntityManager em = JpaUtil.getEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			em.persist(request);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
		} finally {
			em.close();
		}

	}

	public static void salvaUpdate(RequestServiceSabatec request) {

		EntityManager em = JpaUtil.getEntityManager();
		EntityTransaction tx = em.getTransaction();

		try {
			tx.begin();
			request.setId(request.getId());
			request.setCliente(request.getCliente());
			em.merge(request);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			em.close();
		}

	}

	@SuppressWarnings("unchecked")
	public static List<RequestServiceSabatec> listarTodos() throws Exception {

		EntityManager em = JpaUtil.getEntityManager();

		try {
			Query q = em.createNamedQuery("RequestServiceSabatec.findAll");
			return q.getResultList();
		} finally {
			em.close();
		}

	}

	public static void exclui(RequestServiceSabatec request) throws Exception {

		EntityManager em = JpaUtil.getEntityManager();
		try {
			request = em.find(RequestServiceSabatec.class, request.getId());
			em.getTransaction().begin();
			em.remove(request);
			em.getTransaction().commit();

		} catch (Exception e) {
			// TODO: handle exception
			em.getTransaction().rollback();
		} finally {

			em.close();
		}

	}
}
